//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/ienum/package/payment_type_enum.dart';
import 'package:hktk_app/model/package/column_package_model.dart';
import 'package:hktk_app/model/package/order_model.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/util/dao_util.dart';

/// 购买页面相关接口
class PackageDao {
  /// 根据考试查询套餐列表
  static Future<List<PackageModel>> listByExam({
    required String examId,
  }) async {
    Map<String, dynamic> params = {
      "examId": examId,
    };
    String url = "/exam/package/list-by-exam";
    // 发送请求
    var data = await httpPOST(
      url: url,
      tips: "根据考试查询套餐列表",
      params: params,
      contentType: cTypeX,
    );
    bool ss = data is List<dynamic>;

    if (ss) {
      return data.map((dynamic v) => PackageModel.fromJson(v)).toList();
    }
    return <PackageModel>[];
  }

  /// 根据套餐查询套餐sku
  static Future<List<SkuModel>> packageSkuList({
    int size = 1000,
    required String packageId,
  }) async {
    String url = "/exam/package/sku/list?size=$size&packageId=$packageId";
    // 发送请求
    var data = await httpGET(url: url, tips: "根据套餐查询套餐sku");
    if (data is List<dynamic>) {
      return data.map((v) => SkuModel.fromJson(v)).toList();
    }
    return <SkuModel>[];
  }

  /// 通过考试id，科目id，栏目id获取第一个符号套餐信息 科目信息 skuAttr信息
  static Future<ColumnPackageModel> getPackage({
    required String columnId,
    String? examId,
    int? categoryId,
  }) async {
    String url = "/exam/package/getPackage?columnId=$columnId";
    if (examId != null) {
      url = "$url&examId=$examId";
    }
    if (categoryId != null) {
      url = "$url&categoryId=$categoryId";
    }
    // 发送请求
    var data = await httpGET(
      url: url,
      tips: "通过考试id，科目id，栏目id获取第一个符号套餐信息 科目信息 skuAttr信息",
    );
    if (data != null) {
      return ColumnPackageModel.fromJson(data);
    }
    return ColumnPackageModel();
  }

  /// 创建订单
  static Future<OrderModel> createOrder({
    required List<String> skuIds,
    String remark = "",
  }) async {
    Map<String, dynamic> params = {
      "skuIds": skuIds,
      "remark": remark,
    };
    String url = "/order/or/order/create-order";
    // 发送请求
    var data = await httpPOST(url: url, params: params, tips: "创建订单");
    if (data != null) {
      return OrderModel.fromJson(data);
    }
    return OrderModel();
  }

  /// 获取第三方支付签名
  static Future<Map<String, dynamic>> paySign({
    required PaymentTypeEnum type,
    required String orderId,
    String aType = "app",
    DeviceTypeEnum? paymentChannel,
    String? code,
    String? openid,
  }) async {
    String url =
        "/order/pay/sign?type=${type.name}&orderId=$orderId&aType=$aType";
    if (paymentChannel != null) {
      url = "$url&paymentChannel=${paymentChannel.name}";
    }
    if (code != null) {
      url = "$url&code=$code";
    }
    if (openid != null) {
      url = "$url&openid=$openid";
    }
    // 发送请求
    var data = await httpGET(url: url, tips: "获取第三方支付签名");
    return data;
  }

  /// 苹果支付成功回调
  static Future<void> applySuccessNotify({required String orderId}) async {
    Map<String, dynamic> params = {
      "order_id": orderId,
    };
    // 请求路径
    String url = "/order/pay/apply-success-notify";
    // 发送请求
    await httpPOST(
      url: url,
      params: params,
      tips: "苹果支付回调",
    );
  }
  static Future<dynamic> listWithColumnType(Map<String, dynamic> params){
    String url = "/exam/package/list-with-column-type";
    return httpGET1(url: url, params: params);
  }
}
